<?php

include_once '../include/core/image.php';
include_once dirname(dirname(__FILE__)) . '/include/common.php';

if (isset($_POST["main_photo"])) {
    //get photo from link
    if ($_POST["main_photo"] == "") {
        echo json_encode(array('type' => 1, 'msg' => "Vui lòng điền link photo!"));
        die;
    }
    $url_photo = urldecode($_POST["main_photo"]);
    if (!filter_var($url_photo, FILTER_VALIDATE_URL)) {
        echo json_encode(array('type' => 1, 'msg' => "Link photo không hợp lệ. Vui lòng thử lại link khác!"));
        die;
    }

    $image_src = @file_get_contents($url_photo);
    if ($image_src) {
        $temp_file_name = time() . rand(10000, 99999) . session_id() . '.jpg';
        $today = getdate();
        $folder_temp = $today["mday"] . "_" . $today["mon"] . "_" . $today["year"];
        if (!is_dir(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp)) {
            mkdir(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp);
        }
        $temp_path = dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp . DS . $temp_file_name;

        if (!file_put_contents($temp_path, $image_src)) {
            echo json_encode(array('type' => 1, 'msg' => "Lưu ảnh thất bại, vui lòng thử lại!"));
            die;
        } else {
            $path_img_thumb = dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp . DS . "200_" . $temp_file_name;
            copy($temp_path, $path_img_thumb);
            list ( $w, $h ) = getimagesize($temp_path);
            if ($w < 120 || $h < 120) {
                echo json_encode(array('type' => 1, 'msg' => "Ảnh quá bé, chiều rộng và cao của ảnh phải lớn hơn 120px!"));
                die;
            }
            chmod(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp, 0777);
            chmod($temp_path, 0777);
            //create image thumb
            $img_processor = new OSC_Image ();
            $img_processor->setImage($path_img_thumb)->setJpgQuality(100);
            $img_processor->resize(200)->save();
            echo json_encode(array('type' => 0, 'folder_temp' => $folder_temp, 'file_name_temp' => $temp_file_name,
                'thumb_url' => 'tmp_photo' . "/" . $folder_temp . "/" . '200_' . $temp_file_name));
            die;
        }
    } else {
        echo json_encode(array('type' => 1, 'msg' => "Lấy ảnh thất bại, vui lòng thử lại!"));
        die;
    }
} else {
    //upload from form
    if ($_SERVER["CONTENT_LENGTH"] / (1024 * 1024) < 8) {
        $core = new core();
        $temp_name = $_FILES['main_photo']['tmp_name'];
        $img_parts = pathinfo($_FILES['main_photo']['name']);
        $ext = strtolower($img_parts['extension']);

        $allowed_ext = array('gif', 'jpg', 'jpeg', 'png');
        if (!in_array($ext, $allowed_ext)) {
            echo '<p class="uperror">Invalid file!</p>';
            exit;
        }
        $temp_file_name = time() . rand(10000, 99999) . session_id() . '.' . $ext;
        $today = getdate();
        $folder_temp = $today["mday"] . "_" . $today["mon"] . "_" . $today["year"];
        if (!is_dir(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp)) {
            mkdir(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp);
        }
        $temp_path = dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp . DS . $temp_file_name;
        try {

            if (!move_uploaded_file($temp_name, $temp_path)) {
                echo '<p class="uperror">Upload file is failed.</p>';
                exit;
            } else {
                $path_img_thumb = dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp . DS . "200_" . $temp_file_name;
                copy($temp_path, $path_img_thumb);
                list ( $w, $h ) = getimagesize($temp_path);
                if ($w < 120 || $h < 120) {
                    echo '<p class="uperror">Your image does not meet the requirements. Please make sure the width and height are at least 120px!</p>';
                    exit;
                }
                chmod(dirname(dirname(__FILE__)) . DS . "tmp_photo" . DS . $folder_temp, 0777);
                chmod($temp_path, 0777);
                //create image orginal -->jpg
                $img_processor = new OSC_Image ();
                $img_processor->setImage($temp_path)->setJpgQuality(100);
                $img_processor->save();
                //create image thumb
                $img_processor->setImage($path_img_thumb)->setJpgQuality(100);
                $img_processor->resize(200)->save();
                echo '  <p class="folder_temp">' . $folder_temp . '</p>
                    <p class="file_name_temp">' . $temp_file_name . '</p>
                    <p class="thumb_url">tmp_photo' . "/" . $folder_temp . "/" . '200_' . $temp_file_name . '</p>';
                exit;
            }
        } catch (Exception $e) {
            echo '<p class="uperror">' . $e->getMessage() . '</p>';
            exit;
        }
    } else {
        echo '<p class="uperror">Your file exceeds the size limit. Please make sure it is not larger than 8MB</p>';
        exit;
    }
}
?> 